package com.cxt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cxt.dto.WayVo;
import com.cxt.entity.TripData;
import com.cxt.entity.TripInfo;
import com.cxt.result.Result;
import com.cxt.service.*;
import com.cxt.vo.TripDataJkVo;
import com.cxt.vo.TripDataVo;
import com.cxt.vo.TripInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 行程信息表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2025-02-27
 */
@RestController
@RequestMapping("/tripInfo")
@Slf4j
public class TripInfoController {
    @Autowired
    private ITripInfoService tripInfoService;
    @Autowired
    private ITripDataService tripDataService;
    @Autowired
    private IWaypointInfoService waypointInfoService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IMessageNotificationService messageService;
    /**
     * 查询属于自己的行程
     */
    @GetMapping("/get")
    public Result getTripInfo() {
        log.info("查询属于自己的行程");
        List<TripInfoVo> tripInfoList = tripInfoService.getTripInfo();
        return Result.success(tripInfoList);
    }
    /**
     * 查询历史行程信息
     */
    @GetMapping("/getEnd")
    public Result getTripInfoEnd() {
        log.info("查询历史行程信息");
        List<TripInfoVo> tripInfoList = tripInfoService.getTripInfoEnd();
        return Result.success(tripInfoList);
    }

    /**
     * 退出行程
     */
    @GetMapping("/quit")
    public Result quitTripInfo(Long id) {
        log.info("退出行程");
        boolean isQuit = tripInfoService.quit(id);
        if (!isQuit) {
            return Result.error("退出失败");
        }
        return Result.success("退出成功");
    }
    /**
     * 开始行程
     */
    @GetMapping("/start")
    public Result startTripInfo(Long id) {
        log.info("开始行程");
        boolean isStart = tripInfoService.start(id);
        if (!isStart) {
            return Result.error("开始失败");
        }
        return Result.success("开始成功");
    }
    /**
     * 途径点送达判断
     */
    @PostMapping("/waypoint")
    public Result waypointStatus(@RequestBody WayVo wayVo){
        log.info("行程点判断");
        Boolean isWaypoint =tripInfoService.waypointStatus(wayVo);
        return Result.success(isWaypoint);
    }
    /**
     * 用户求助
     */
    @GetMapping("/help")
    public Result helpTripInfo(Long id) {
        log.info("用户求助");
        //查找所有管理员用户
        Boolean  isHelp = messageService.helpTripInfo(id);
        if (!isHelp) {
            return Result.error("求助失败");
        }
        return Result.success("求助成功");
    }

    /**
     * 保存实时行程
     */
    @PostMapping("/putWay")
    public Result putWaypoint(@RequestBody TripData tripData) {
        log.info("添加实时行程");
        //设置当前时间
        tripData.setCreateTime(LocalDateTime.now());
        boolean isPut = tripDataService.save(tripData);
        if (!isPut) {
            return Result.error("添加失败");
        }
        return Result.success();
    }
    /**
     * 获取实时行程
     */
    @GetMapping("/getWay")
    public Result getWaypoint(Long tripId) {
        log.info("获取实时行程");
        //是tripId的，且时间是最新的
        LambdaQueryWrapper<TripData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TripData::getTripId, tripId)
                .orderByDesc(TripData::getCreateTime)
                .last("limit 1");
        TripData tripData = tripDataService.getOne(queryWrapper);
        if(tripData==null){
            return Result.success("没有数据");
        }
        TripDataVo tripInfoVo = new TripDataVo(tripData, waypointInfoService.getById(tripData.getNextWaypointId()));
        return Result.success(tripInfoVo);
    }
    /**
     * 获取所有实时行程
     */
    @GetMapping("/getAllWay")
    public Result getALl() {
        log.info("获取所有实时行程");
        List<TripDataJkVo> list = tripDataService.getList();
        return Result.success(list);
    }
    /**
     * 删除
     */
    @GetMapping("/deleteWay")
    public Result deleteWaypoint(Integer tripId) {
        log.info("删除实时行程");
        //删除所有tripID
        boolean isDelete = tripDataService.remove(new LambdaQueryWrapper<TripData>().eq(TripData::getTripId, tripId));
        if (!isDelete) {
            return Result.error("删除失败");
        }{
            return Result.success("删除成功");
        }
    }
    /**
     * 添加行程信息表
     */
    @PostMapping("/add")
    public Result addTripInfo(@RequestBody TripInfo tripInfo) {
        log.info("添加行程信息表");
        boolean isAdd = tripInfoService.save(tripInfo);
        if (!isAdd) {
            return Result.error("添加失败");
        }
        return Result.success();
    }

    /**
     * 根据id删除行程信息表
     */
    @GetMapping("/delete")
    public Result deleteTripInfo(Integer id) {
        log.info("删除行程信息表,id:{}", id);
        boolean isDelete = tripInfoService.removeById(id);
        if (!isDelete) {
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    /**
     * 修改行程信息表
     */
    @PostMapping("/update")
    public Result updateTripInfo(@RequestBody TripInfo tripInfo) {
        log.info("修改行程信息表");
        boolean isUpdate = tripInfoService.updateById(tripInfo);
        if (!isUpdate) {
            return Result.error("修改失败");
        }
        return Result.success();
    }

}
